package horde.internalResources.horrorControl.HCvis;

import horde.internalResources.Directory;
import horde.internalResources.HorrorControl.HorrorControl;
import horde.internalResources.dictionaries.dictionary;
import horde.internalResources.horrorControl.HCvis.components.panelCelda;
import horde.patterns.Core;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.lang.reflect.Method;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 * Component that provides  dynamic debugging the application
 * @author Arturo Mazón and Andres Picazo
 */
public class debugPanel extends JPanel {

    // <editor-fold defaultstate="collapsed" desc="Builders">
    public debugPanel() {
        super();
        createElements();
        organizeElements();
        crearMouseListener();
        list.addMouseListener(mouseListener);
        this.setSize(tamPanel);
        this.setVisible(true);
    }

    public debugPanel(HorrorControl hc) {
        super();
        this.hc = hc;
        createElements();
        organizeElements();
//        crearMouseListener();
//        list.addMouseListener(mouseListener);
        this.setSize(tamPanel);
        this.setVisible(true);
    }

    // </editor-fold>
    
    /**
     * Creates all the components necessary to build the graphic interface
     */
    private void createElements() {
        list = new JList();
        tracesPList = new JPanel();
        DefaultListModel modelL = listModel();
        list.setModel(modelL);
        list.addListSelectionListener(new SharedListSelectionHandler());
        tracesPList.setPreferredSize(new Dimension(100, 500));
        labelListDebug.setForeground(Color.blue);
        tracesPList.add(labelListDebug);
        tracesPList.add(list);
        modelt = new DefaultTableModel() {

            @Override
            public Class getColumnClass(int columnindex) {
                return panelCelda.class;
            }
        };
        modelt.addColumn("Methods");
        table = new JTable();
        table.setDefaultRenderer(panelCelda.class, new panelCelda());
        table.setDefaultEditor(panelCelda.class, new panelCelda());
        table.setModel(modelt);

//        prueba = new JPanel();


        table.setRowHeight(200);

        tablePanel = new JPanel(new BorderLayout());
        tablePanel.add(table, BorderLayout.CENTER);
    }

    /**
     * Places properly each component in the graphic interface
     */
    private void organizeElements() {
        this.setLayout(new BorderLayout());
        this.add(tracesPList, BorderLayout.WEST);
        JScrollPane scroll = new JScrollPane(table);

        this.add(scroll, BorderLayout.CENTER);
    }

    // <editor-fold defaultstate="collapsed" desc="List of agents management">
    /**
     * Creates a standard model for the list of agents that have traced something
     * @return DefaultListModel
     */
    private DefaultListModel listModel() {
        DefaultListModel modelL = new DefaultListModel();
        return modelL;
    }

    /**
     * Adds a new agent to the list of agents in the graphic interface
     * @param o Agent given
     */
    public void setJListModel(Object o) {
        if (!list.getModel().toString().contains((String) o)) {
            DefaultListModel modelL = (DefaultListModel) list.getModel();
            modelL.addElement(o);
            list.setModel(modelL);
        }
    }

    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Listeners">
    /**
     * Not implemented yet
     */
    private void crearMouseListener() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Class/method that implements the listener for the list of agents. Multiselect
     */
    protected class SharedListSelectionHandler implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            if (!list.isSelectionEmpty()) {
                String s = list.getSelectedValue().toString();

                Core c = Directory.getInstance().getAgentCore(s);

                if (c != null) {
                    while (modelt.getRowCount() > 0) {
                        modelt.removeRow(0);
                    }
                    Method[] methods = c.getClass().getDeclaredMethods();
                    for (int i = 0; i < methods.length; i++) {
                        panelCelda p = new panelCelda(methods[i].getName());
                        p.setMsgComboBox(Directory.getInstance().getAutomaton(s).getStates());

                        Object[] aux = {methods[i]};
                        p.setDataTable(methods[i], list.getSelectedValue().toString());
//                            System.err.println(list.getSelectedValue().toString());
                        panelCelda[] data = {p};
                        modelt.addRow(data);
                    }
                } else {
                    while (modelt.getRowCount() > 0) {
                        modelt.removeRow(0);
                    }
                }
            }

        }
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Atributes">
    private static HorrorControl hc = null;
    private Dimension tamPanel = new Dimension(400, 500);
    protected JList list;
    private JLabel labelListDebug = new JLabel(dictionary.getString("hcVis.labelListDebug"));
    private MouseListener mouseListener;
    private JPanel tracesPList;
    private JPanel tablePanel;
    private JTable table;
    private DefaultTableModel modelt;

    // </editor-fold>
}
